Code for the Parking Lot
Let's write the code for the classes that we have designed, in different languages in this lesson.
We’ve gone over the different aspects of the parking lot system and observed the attributes attached to the problem using various UML diagrams. Let’s explore the more practical side of things, where we will work on implementing the parking lot system using multiple languages. This is usually the last step in an object-oriented design interview process.
We have chosen the following languages to write the skeleton code of the different classes present in the parking lot system:
Java
C#
Python
C++
JavaScript
Parking lot classes#
In this section, we will provide the skeleton code of the classes designed in the class diagram lesson.
Note: For simplicity, we aren’t defining getter and setter functions. The reader can assume that all class attributes are private and accessed through their respective public getter methods and modified only through their public method functions.
Enumerations and custom data type#
First of all, we will define all the enumerations required in the parking lot. According to the class diagram, there are two enumerations used in the system i.e., PaymentStatus
and AccountStatus
. The code to implement these enumerations and custom data types is as follows:
Note: JavaScript does not support enumerations, so we will be using the
Object.freeze()
method as an alternative that freezes an object and prevents further modifications.
Parking spots#
The first section of the parking lot system that we will work on is the ParkingSpot
class, which will act as a base class for four different types of parking spots: handicapped, compact, large, and motorcycle. This will have an instance of the Vehicle
class. The definition of the ParkingSpot
class and the classes being derived from it are given below:
Vehicle#
Vehicle
will be another abstract class, which serves as a parent for four different types of vehicles: car, truck, van, and motor cycle. The definition of the Vehicle
and its child classes are given below:
Account#
The Account
class will be an abstract class, which will have the actors, Admin
and ParkingAttendant
, as child classes. The definition of these classes is given below:
Display board and parking rate#
This section contains the DisplayBoard
and ParkingRate
classes that only have the composition class with the ParkingLot
class. This relationship is highlighted in the ParkingLot
class. The definition of these classes is given below:
Entrance and exit#
This section contains the Entrance
and Exit
classes, both of which are associated with the ParkingTicket
class. The definition of the Entrance
and Exit
classes is given below:
Parking ticket#
The definition of the ParkingTicket
class can be found below. This contains instancesof the Vehicle
, Payment
, Entrance
and Exit
classes:
Payment#
The Payment
class is another abstract class, with the Cash
and CreditCard
classes as its child. This takes the PaymentStatus
enumeration and the dateTime
data type to keep track of the payment status
and time. The definition of this class is given below
Parking lot#
The final class of the parking lot system is the ParkingLot
class which will be a Singleton class, meaning the entire system will only have one instance of this class. The definition of this class is given below:
Wrapping up#
We've explored the complete design of a parking lot system in this chapter. We've looked at how a basic parking lot system can be visualized using various UML diagrams and designed using object-oriented principles and design patterns.
Activity Diagram for the Parking Lot
Getting Ready: Elevator System